package org.example;

import org.example.ioc.annotation.ComponentScan;
import org.example.ioc.context.ApplicationContext;
import org.example.service.CountdownService;
import org.example.service.TimeDisplayService;

import java.util.concurrent.CompletableFuture;

@ComponentScan(basePackages = "org.example.service")
public class Main {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("启动IOC容器...");
        ApplicationContext context = new ApplicationContext(Main.class);
        
        // 打印注册的Beans用于调试
        context.printRegisteredBeans();
        
        System.out.println("从容器中获取倒计时服务...");
        CountdownService countdownService = context.getBean("countdownService", CountdownService.class);
        
        System.out.println("从容器中获取时间显示服务...");
        TimeDisplayService timeDisplayService = context.getBean("timeDisplayService", TimeDisplayService.class);
        
        // 添加空值检查
        if (countdownService == null) {
            System.err.println("错误: 无法获取CountdownService实例");
            return;
        }
        
        if (timeDisplayService == null) {
            System.err.println("错误: 无法获取TimeDisplayService实例");
            return;
        }
        
        System.out.println("开始倒计时任务...");
        countdownService.startCountdown(10);
        
        // 异步获取时间
        timeDisplayService.displayCurrentTimeAsync();
        
        // 使用CompletableFuture获取时间
        CompletableFuture<String> futureTime = timeDisplayService.getCurrentTimeAsync();
        futureTime.thenAccept(time -> System.out.println("Future获取的时间: " + time));
        
        // 等待任务完成
        Thread.sleep(12000);
        
        // 关闭服务
        timeDisplayService.shutdown();
        
        System.out.println("程序结束");
    }
}